/*
 * @Author: jjjjwei 1506865174@qq.com
 * @Date: 2022-08-04 11:38:40
 * @LastEditors: jjjjwei 1506865174@qq.com
 * @LastEditTime: 2022-08-11 09:11:49
 * @FilePath: \react\react-routemanage\src\utils\router.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { useRoutes } from 'react-router-dom';
import Login from '../page/Login'
import Home from '../page/Home'
import LoadableComponent  from '@loadable/component'
import {useSelector} from 'react-redux'
const SetRoutes = () => {
  const {user} = useSelector(state=>state.userReducer)
  function bindRouter(list){
    let arr = [];
      arr = list.map((item)=>{
        const ComponentNode = LoadableComponent(()=>{
            return import("../component/"+item.componentPath)
        })
          if(item.menuChilds && item.menuChilds.length>0){
            if(item.isContainChildren){
                return({
                    path:item.pathRoute,
                    element:<ComponentNode/>,
                    children:[...bindRouter(item.menuChilds)]
                })
            }else{
              return({
                path:item.pathRoute,
                // element:<ComponentNode/>,
                children:[...bindRouter(item.menuChilds)]
            })
            }
          }else{
            return({
                path:item.pathRoute,
                element:<ComponentNode/>
            })
          }
      })
      return arr;
   }
  //  console.log(bindRouter(user.menuInfo?user.menuInfo:[]),8888);
  return useRoutes([
    {
      path:'/',
      element: <Login/>
    },
    {
      path:'/home', 
      element: <Home/>,
      children:[...bindRouter(user.menuInfo?user.menuInfo:[])]
    },
  ]);
}

export default SetRoutes